#include "./minithemewnd.h"
#include "./themedraw.h"

using namespace MiniUI;
using namespace MiniUI::Type;

MiniThemeWnd::MiniThemeWnd()
{
    m_pWinView = WinView::CreateInstance();
    m_pWinView->SetWinBorder(MiniRect(0, 0, 0, 0));
    m_pWinView->OpenWindow(MiniRect(100, 100, 689, 546));

    Image* pBkg = Image::CreateInstance();
    pBkg->SetValue("wnd_bkg.png");
    pBkg->SetAnchors(UIObject::ANCHOR_LEFT | UIObject::ANCHOR_TOP | UIObject::ANCHOR_RIGHT | UIObject::ANCHOR_BOTTOM);
    pBkg->SetMargin(MiniRect(0, 0, 0, 0));
    pBkg->SetTransparent(true);
    m_pWinView->AddChild(pBkg);
    pBkg->Release();

    Button* pBtnClose = Button::CreateInstance();
    pBtnClose->OnClick += EventObject<OnClickEventFunc>(this, &MiniThemeWnd::OnBtnCloseClick);
    pBtnClose->Init("btnclose_normal.png", "btnclose_hover.png", "btnclose_down.png");
    m_pWinView->AddChild(pBtnClose);
    pBtnClose->SetAnchors(UIObject::ANCHOR_TOP | UIObject::ANCHOR_RIGHT);
    pBtnClose->SetMargin(MiniRect(0, 1, 3, 0));
    pBtnClose->Release();

    //draw theme

    //draw button
    DrawTheme(MiniPoint(20, 50), MiniSize(40, 40), BUTTON);

    //draw radio
    DrawTheme(MiniPoint(20, 100), MiniSize(40, 40), RADIO);

    //draw checkbox
    DrawTheme(MiniPoint(20, 150), MiniSize(40, 40), CHECKBOX);

    //draw scrollbar
    DrawTheme(MiniPoint(20, 200), MiniSize(150, 30), SCROLLBAR);
}

MiniThemeWnd::~MiniThemeWnd()
{
    m_pWinView->Release();
}

void MiniThemeWnd::OnBtnCloseClick(const UIObject* sender, const Type::MiniPoint& pt, MouseButton button, uint keyState)
{
    ::PostQuitMessage(0);
}

void MiniThemeWnd::DrawTheme(MiniPoint pos, MiniSize size, ControlStyle style)
{
    static ThemeDraw theme;
    Image* pImgTheme = Image::CreateInstance();
    m_pWinView->AddChild(pImgTheme);
    pImgTheme->SetPos(pos);

    Bitmap* pBitmap = Bitmap::CreateInstance();
    switch (style)
    {
    case BUTTON:
        pBitmap->Rebuild(size, theme.PaintButton(size.cx, size.cy));
        break;
    case RADIO:
        pBitmap->Rebuild(size, theme.PaintRadio(size.cx, size.cy));
        break;
    case CHECKBOX:
        pBitmap->Rebuild(size, theme.PaintCheckbox(size.cx, size.cy));
        break;
    case SCROLLBAR:
        pBitmap->Rebuild(size, theme.PaintScrollBar(size.cx, size.cy));
        break;
    }

    pImgTheme->SetBitmap(pBitmap);
    pImgTheme->SetSize(size);
    pBitmap->Release();
    pImgTheme->Release();
}